package com.iocollege.tinyframe.mapper.system;

import com.iocollege.tinyframe.common.core.MyMapper;
import com.iocollege.tinyframe.entity.system.SysUserRole;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 用户和角色关联表 Mapper 接口
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-03-22
 */
public interface SysUserRoleMapper extends MyMapper<SysUserRole> {

    /**
     * 通过用户ID删除用户和角色关联
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Delete("delete from sys_user_role where user_id=#{userId}")
    public int deleteUserRoleByUserId(Long userId);

    /**
     * 批量删除用户和角色关联
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Delete("<script>delete from sys_user_role where user_id in\n" +
            " \t\t<foreach collection=\"array\" item=\"userId\" open=\"(\" separator=\",\" close=\")\">\n" +
            " \t\t\t#{userId}\n" +
            "        </foreach> </script>")
    public int deleteUserRole(Long[] ids);

    /**
     * 通过角色ID查询角色使用数量
     *
     * @param roleId 角色ID
     * @return 结果
     */
    @Select(" select count(1) from sys_user_role where role_id=#{roleId}  ")
    public int countUserRoleByRoleId(Long roleId);

    /**
     * 批量新增用户角色信息
     *
     * @param userRoleList 用户角色列表
     * @return 结果
     */
    @Insert("<script>insert into sys_user_role(user_id, role_id) values\n" +
            "\t\t<foreach item=\"item\" index=\"index\" collection=\"list\" separator=\",\">\n" +
            "\t\t\t(#{item.userId},#{item.roleId})\n" +
            "\t\t</foreach>" +
            "</script>")
    public int batchUserRole(List<SysUserRole> userRoleList);

    /**
     * 删除用户和角色关联信息
     *
     * @param userRole 用户和角色关联信息
     * @return 结果
     */
    @Delete("delete from sys_user_role where user_id=#{userId} and role_id=#{roleId}")
    public int deleteUserRoleInfo(SysUserRole userRole);

    /**
     * 批量取消授权用户角色
     *
     * @param roleId 角色ID
     * @param userIds 需要删除的用户数据ID
     * @return 结果
     */
    @Delete("<script> delete from sys_user_role where role_id=#{roleId} and user_id in\n" +
            " \t    <foreach collection=\"userIds\" item=\"userId\" open=\"(\" separator=\",\" close=\")\">\n" +
            " \t        #{userId}\n" +
            "            </foreach> </script>")
    public int deleteUserRoleInfos(@Param("roleId") Long roleId, @Param("userIds") Long[] userIds);

}
